//
//  HomeTableViewController.swift
//  SinaWeibo
//
//  Created by mac on 16/2/14.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit
import AFNetworking
import SVProgressHUD
class HomeTableViewController: baseTableViewController {
    //模型数组
    private lazy var statuses = [Status]()
    //标示符
    private let homeCellId = "homeCellId"
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        if !userLogin{
        visitorLoginViews?.setupInfoWtih("关注一些人，回这里看看有什么惊喜", imageName: nil)
        return
        }//print(view)
        prepareForTableView()
        loadData()
    }
    
    private func prepareForTableView() {
        //注册cell
//        tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: homeCellId)
        tableView.registerClass(StatusCell.self, forCellReuseIdentifier: homeCellId)
        //设置Cell的高度预计
//        tableView.rowHeight = 300
        tableView.estimatedRowHeight = 300
        //自动计算行高
        tableView.rowHeight = UITableViewAutomaticDimension
        //设置分割线样式
        tableView.separatorStyle = .None

    }

//    override func viewWillLayoutSubviews() {
//        super.viewWillLayoutSubviews()
//        //print(view)
//    }
    private func loadData(){
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        guard let token = AccountViewModel.sharedAccountViewModel.access_token else {
            SVProgressHUD.showErrorWithStatus("请登录")
            return
            
        }
        let parameters = ["access_token":token]
        let AFN = AFHTTPSessionManager()
        AFN.GET(urlString, parameters: parameters, progress: nil, success: { (_, result) -> Void in
            guard let dict = result as? [String : AnyObject] else {
                SVProgressHUD.showErrorWithStatus("数据类型不匹配")
                return
            }
            //在字典中通过  "statuses"这个key 来去获取微博数组数据
            guard let array = dict["statuses"] as? [[String : AnyObject]] else {
                SVProgressHUD.showErrorWithStatus("数据类型不匹配")
                return
            }
            //实例化数据模型
            var tempArray = [Status]()
            //遍历数组获取单个的字典对象
            for item in array{
                let s = Status(dict: item)
                tempArray.append(s)
            }
            //记录临时数组
            self.statuses = tempArray
        //刷新tableView
            self.tableView.reloadData()
            }) { (_, error) -> Void in
                SVProgressHUD.showErrorWithStatus("网路出错")
        }
    }
}
//数据源方法
extension HomeTableViewController {
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statuses.count
    }
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//        let cell = tableView.dequeueReusableCellWithIdentifier(homeCellId, forIndexPath: indexPath)
        let cell = tableView.dequeueReusableCellWithIdentifier(homeCellId, forIndexPath: indexPath) as!StatusCell

        //cell.textLabel?.text = statuses[indexPath.row].user?.name
        cell.status = statuses[indexPath.row]
        return cell
    }
    
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
